from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
from config import HOST, PORT, RELOAD, STATIC_DIR, LOG_CONF

from api.knowledge import router as knowledge_router
from api.sessions import router as sessions_router
from api.query import router as query_router

# 初始化FastAPI应用
app = FastAPI(title="RAG 知识库问答系统")

# 挂载静态文件目录
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")

# 注册路由
app.include_router(knowledge_router)
app.include_router(sessions_router)
app.include_router(query_router)

# 首页路由
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
    """返回主页面"""
    from fastapi.templating import Jinja2Templates
    templates = Jinja2Templates(directory=STATIC_DIR)
    return templates.TemplateResponse("index.html", {"request": request})

@app.get("/knowledge.html", response_class=HTMLResponse)
async def read_knowledge_page(request: Request):
    """返回知识库管理页面"""
    from fastapi.templating import Jinja2Templates
    templates = Jinja2Templates(directory=STATIC_DIR)
    return templates.TemplateResponse("knowledge.html", {"request": request})

# # 应用启动时初始化数据库
# @app.on_event("startup")
# async def startup_event():
#     await init_database()
#     print("数据库初始化完成")

if __name__ == "__main__":
    uvicorn.run("main:app", host=HOST, port=PORT, reload=RELOAD, log_config=LOG_CONF)
